package io.renren.modules.generator.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Hashtable;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.annotation.LoginUser;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.generator.entity.BsnMentorEntity;
import io.renren.modules.generator.entity.BsnStudentEntity;
import io.renren.modules.generator.service.BsnMentorService;
import io.renren.modules.generator.service.BsnStudentService;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.ShortUuidUtils;



/**
 * 学生表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-06-16 23:45:56
 */
@RestController
@RequestMapping("generator/bsnstudent")
public class BsnStudentController extends AbstractController {
    @Autowired
    private BsnStudentService bsnStudentService;

    @Autowired
    private BsnMentorService bsnMentorService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestBody Map<String, Object> params){
    	params.put("sidx", "createTime");
    	params.put("order", "desc");
        PageUtils page = bsnStudentService.queryPage(params);

        return R.ok().put("page", page);
    }
    
    /**
     * 根据指导老师查询学生列表
     */ 
    @Login
    @RequestMapping("/listByMentor")
    public R listByMentor(@RequestBody Map<String, Object> params, @LoginUser SysUserEntity loginUser){
    	String mentorId = "8";
    	if (params.containsKey("mentorId")) { 
    		mentorId = params.get("mentorId").toString();
    	} else {
//    		这有问题，登陆后查不到用户需要修改。
    		if (loginUser != null) {
        		mentorId = bsnMentorService.selectByLoginId(
        				loginUser.getUserId().toString()).getId();    			
    		}
  
    	}
    	
    	params.put("sidx", "createTime");
    	params.put("order", "desc");
        PageUtils page = bsnStudentService.queryPage(params,
        		new EntityWrapper<BsnStudentEntity>().eq("daoshiid", mentorId));

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
			BsnStudentEntity bsnStudent = bsnStudentService.selectById(id);

        return R.ok().put("bsnStudent", bsnStudent);
    }
 
    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody BsnStudentEntity bsnStudent){
    	bsnStudent.setId(ShortUuidUtils.generateShortUuid());
    	bsnStudent.setCreateTime(new Date());
    	bsnStudentService.insert(bsnStudent);

        return R.ok().put("bsnStudent", bsnStudent);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody BsnStudentEntity bsnStudent){
			bsnStudentService.updateById(bsnStudent);

        return R.ok();
    }
    /**
     * 修改登录id
     */
    @RequestMapping("/updateLoginId")
    public R updateLoginId(@RequestBody Map<String, Object> params){
    	BsnStudentEntity entity = new BsnStudentEntity();
    	String loginId = params.get("loginId").toString();
    	String mentorId = params.get("id").toString();
    	
    	entity.setLoginId(loginId);
    	bsnStudentService.update(entity, 
				new EntityWrapper<BsnStudentEntity>().eq("id", mentorId));
        return R.ok();
    }
    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] ids){
			bsnStudentService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

    
    /**
     * 根据登录的家长id查找儿童信息
     */
    @Login
    @RequestMapping("/infoByLoginParent")
    public R infoByLoginParent(@LoginUser SysUserEntity loginUser){
    	String loginId = "8";
    	if (loginUser != null) {
    		loginId = loginUser.getUserId().toString();
    		logger.info("loginId={}", loginUser.getUserId());
    	}
    	BsnStudentEntity entity = new BsnStudentEntity();
    	entity.setLoginId(loginId);
    	Map<String, Object> params = new Hashtable<String, Object>();
    	params.put("limit", "100");
    	params.put("page", "1");
    	params.put("sidx", "createTime");
    	params.put("order", "desc");
        PageUtils page = bsnStudentService.queryPage(params, 
        		new EntityWrapper<BsnStudentEntity>().eq("login_id", loginId));

        return R.ok().put("page", page);
    }
    
    /**
     * 根据登录的家长id查找儿童信息
     */
    @RequestMapping("/assignMentor")
    public R assignMentor(@RequestBody Map<String, Object> params){
    	String id = params.get("studentId").toString();
    	String mentorId = params.get("mentorId").toString();
    	
    	BsnStudentEntity entity = new BsnStudentEntity();
    	entity.setDaoshiid(mentorId);
    	bsnStudentService.update(entity, 
				new EntityWrapper<BsnStudentEntity>().eq("id", id));

        return R.ok();
    }
}
